import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:myapp/common_ui/dialog/dialog_factory.dart';
import 'package:myapp/pages/about_us/about_us_page.dart';
import 'package:myapp/pages/auth/login_page.dart';
import 'package:myapp/pages/my_collects/my_collects_page.dart';
import 'package:myapp/pages/personal/personal_vm.dart';
import 'package:myapp/route/route_utils.dart';
import 'package:oktoast/oktoast.dart';
import 'package:provider/provider.dart';

class PersonalPage extends StatefulWidget {

   PersonalPage({super.key});



  @override
  State createState() {
    return _PersonalPageState();
  }
}

class _PersonalPageState extends State<PersonalPage> {
  PersonViewModel viewModel = PersonViewModel();

  @override
  void initState() {
    super.initState();
    viewModel.initData();
  }

  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider(create: (context){
      return viewModel;
    },child: Scaffold(
      body: Column(
        children: [
          _header((){
            if(viewModel.shouldLogin){
              //跳转 登录页
              RouteUtils.push(context, LoginPage());
            }
          }),
          _settingsItem(title: "我的收藏",onTap: (){
            if(viewModel.shouldLogin){
              RouteUtils.push(context, LoginPage());
            }else{
              RouteUtils.push(context, MyCollectsPage());
            }

          }),
          _settingsItem(title:"检查更新",onTap: (){
            checkAppUpdate();
          }),
          _settingsItem(title:"关于我们",onTap: (){
            RouteUtils.push(context, AboutUsPage());
          }),
          Consumer<PersonViewModel>(builder: (context,vm,child){
            if(vm.shouldLogin){
              return SizedBox();
            }
            return _settingsItem(title: "退出登录",onTap: (){
              print("退出登录");
              viewModel.logout((value){
                if(value){
                  RouteUtils.pushAndRemoveUntil(context, LoginPage());
                }
              });
            });
          })
        ],
      ),
    ));
  }

  Widget _settingsItem({String? title,GestureTapCallback? onTap}){
    return Container(
      margin: EdgeInsets.only(left: 15.w,right: 15.w,top: 15.h),
      padding: EdgeInsets.only(left: 10.w,right: 10.w),
      width: double.infinity,
      height: 45.h,
      decoration: BoxDecoration(
        border: Border.all(color: Colors.grey,width: 1.r),
        borderRadius: BorderRadius.all(Radius.circular(5.r))
      ),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          GestureDetector(onTap: onTap,child: Text(title?? "",style: TextStyle(color: Colors.black,fontSize: 13.sp),)),
          GestureDetector(onTap: onTap,child: Image.asset("assets/images/img_arrow_right.png",width: 20.r,height: 20.r))
        ],
      ),
    );
  }

  Widget _header(GestureTapCallback? onTap){
    return Container(
      alignment: Alignment.center,
      color: Colors.teal,
      width: double.infinity,
      height: 200.h,
      child: GestureDetector(onTap: onTap,child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          ClipRRect(
            borderRadius: BorderRadius.all(Radius.circular(35.r)),
            child: Image.asset("assets/images/logo.png",width: 70.r,height: 70.h,fit: BoxFit.fill,),
          ),
          SizedBox(height: 6.h,),
          Consumer<PersonViewModel>(builder: (context,vm,child){
            return GestureDetector(
              onTap: onTap,
              child: Text(vm.username ?? "",style: TextStyle(color: Colors.white,fontSize: 13.sp),),
            );
          })
        ],
      ))
    );
  }

  //检查更新
void checkAppUpdate(){
   viewModel.checkUpdate().then((url){
     if(url != null && url.isNotEmpty ==true){
       DialogFactory.instance.showNeedUpdateDialog(context: context,
           dismissClick: (){
             //是否显示更新红点
             viewModel.shouldShowUpdateDot();
           },
           confirmClick: (){
             viewModel.jumpToOutLink(url);
           }
       );
     }else{
      showToast("已是最新版本");
     }
   });
}

}
